﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Bus;

namespace App.admin
{
    public partial class addconsume : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session[EnumCollections.SESSION_LOGINNAME] != null && !String.IsNullOrWhiteSpace(Convert.ToString(Session[EnumCollections.SESSION_LOGINNAME])))
                {
                    BindConsumeAccounts();
                }
                else
                {
                    Response.Redirect("~/Login.aspx?request=" + Guid.NewGuid().ToString("N"));
                }
            }
        }


        private void BindConsumeAccounts()
        {
            var ds = DbHelperSQLite.Query("SELECT Id,AccountName FROM Account");
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                var li = new ListItem();
                li.Text = dr["AccountName"].ToString();
                li.Value = dr["Id"].ToString();
                this.chklConsumePerson.Items.Add(li);
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            UploadFile();
        }

        private void UploadFile()
        {
            string fullname = this.fileUpload.FileName.ToString();
            if (string.IsNullOrWhiteSpace(fullname))
            {
                Assistant.AlertBox.MessageBox("未选择上传文件！");
                return;
            }
            string filetype = this.fileUpload.PostedFile.ContentType.ToString();
            // string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);
            string size = this.fileUpload.PostedFile.ContentLength.ToString();
            string filename = "";
            if (filetype.Equals("image/gif") || filetype.Equals("image/jpeg") || filetype.Equals("image/png"))
            {
                filename = fullname;
                string savepath = Server.MapPath("~/uploads") + "\\" + filename;
                if (!File.Exists(savepath))
                {
                    this.fileUpload.SaveAs(savepath);
                    //this.hfSavePath.Value = savepath;//仍然保存为相对目录
                    this.hfSavePath.Value = "uploads/" + filename;
                }
                else
                {
                    Assistant.AlertBox.MessageBox("同名文件已经存在！");
                }
            }
            else
            {
                Assistant.AlertBox.MessageBoxThenUrl("文件类型不正确！", "addconsume.aspx");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtConsumeDateTime.Value) || String.IsNullOrEmpty(this.lblAvgCost.Value))
            {
                Assistant.AlertBox.MessageBox("消费日期和消费额不允许为空！");
                return;
            }

            //就餐人员取均值，未就餐人员将消费值设置为0
            DateTime consumeTime = DateTime.Parse(this.txtConsumeDateTime.Value);
            string filepath = this.hfSavePath.Value;
            int consumeCost = Convert.ToInt32(decimal.Parse(this.lblAvgCost.Value) * 100);
            //
            List<Consume> consumes = GetConsumeAccounts(consumeTime, consumeCost, filepath);
            if (consumes != null && consumes.Count > 0)
            {
                foreach (Consume item in consumes)
                {
                    Consume saveItem = new Consume();
                    saveItem.AccountId = item.AccountId;
                    saveItem.Cost = item.Cost;
                    saveItem.ImagePath = item.ImagePath;
                    saveItem.InsertDateTime = item.InsertDateTime;
                    saveItem.Add();
                }
                Assistant.AlertBox.MessageBoxThenUrl("保存成功！","../default.aspx");
            }
        }


        private List<Consume> GetConsumeAccounts(DateTime consumeTime, int consumeCost, string filepath)
        {
            List<Consume> consumes = new List<Consume>();
            foreach (ListItem li in this.chklConsumePerson.Items)
            {
                Consume item = new Consume();
                item.AccountId = int.Parse(li.Value);
                item.InsertDateTime = consumeTime;
                if (li.Selected)
                {
                    item.ImagePath = filepath;
                    item.Cost = consumeCost;
                }
                else
                {
                    item.Cost = 0;
                }
                consumes.Add(item);
            }

            return consumes;
        }

        protected void txtCost_TextChanged(object sender, EventArgs e)
        {

        }
    }
}